// pages/user/mymer/detail/detail.js
const app = getApp()
const utils = require('../../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantDetail: {},
    providerList: [],
    mid: '',
    isEdit: false
  },

  setNum: function(e) {
    let list = this.data.providerList,
        pid = e.currentTarget.dataset.pid

    for (let k in list) {
      if(pid = list[k].provider_id) {
        list[k].project_num = e.detail.value
      }
      this.setData({
        providerList: list
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mid: options.mid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAccounts(this.data.mid)

    // let _this = this
    // let random = Math.floor(Math.random() * 100)
    // if (random < 45) _this.getAccounts(_this.data.mid)
    // else _this.error()
  },

  error: function () {
    wx.showLoading({
      title: '加载数据',
    })
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '请求被拒绝',
        duration: 3000
      })
    }, 5000)
  },

  getAccounts: function (mid) {
    let that = this,
        data, data2
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/order-push/detail?access_token=' + accessToken,
        data: {
          id: mid
        },
        success: res => {
          data = res.data.data
          if(res.data.status === 1) {
            wx.request({
              url: app.globalData.hostAPI + '/v1/order-push/finished-list?access_token=' + accessToken,
              data: {
                id: mid
              },
              success: list => {
                data2 = list.data.data
                if (list.data.status === 1) {
                  if (data.push_order_detail.type === 1) {
                    for(let k in data2) {
                      data2[k].project_num = 1
                    }
                    that.setData({
                      isEdit: true
                    })
                  }
                  data.push_order_detail.service_at = utils.formatTimeThr(data.push_order_detail.service_at, 'Y-M-D')
                  that.setData({
                    merchantDetail: data.push_order_detail,
                    providerList: data2
                  })
                }
              }
            })
          }
        }
      })
      
    })
  },

  /**
   * 
   */
  accounts: function() {
    let _this = this,
        providerList = this.data.providerList,
        dataObj = {},
        arr = []
    // wx.showLoading({
    //   title: '数据加载中',
    // })
    if (providerList.length == 0 ) {
      wx.showToast({
        title: '没有保洁需要结算',
      })
      return false
    }

    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      //dataObj['id'] = _this.data.mid
      for (let k = 0; k < providerList.length; k++) {
        dataObj = {
          provider_id: providerList[k].provider_id,
          project_num: providerList[k].project_num
        }
        if (providerList[k].project_num == 0) {
          wx.showToast({
            title: '工作不能为0',
          })
          return false
        }
        arr.push(dataObj)
      }
      console.log(arr)
      wx.request({
        url: app.globalData.hostAPI + '/v1/order-push/finished?access_token=' + accessToken,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          id: _this.data.mid,
          project_num: JSON.stringify(arr)
        },
        success: res => {
          if(res.data.status === 1) {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
              success: conf => {
                if(conf.confirm) {
                  wx.redirectTo({
                    url: '../mymer',
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              duration: 2000
            })
          }
        }
      })
    })
  }
})